package com.httpclient.getDemo.service.Impl;

import com.httpclient.getDemo.service.GetSimpleDemoService;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;

@Service
public class GetSimpleDemoServiceImpl implements GetSimpleDemoService {



    @Override
    public void getSimpleDemo() {

        CloseableHttpClient closeableHttpClient= HttpClientBuilder.create().build();

        String url="https://www.baidu.com/";

        HttpGet get=new HttpGet(url);
        CloseableHttpResponse response=null;
        //        自定义配置
        RequestConfig requestConfig = RequestConfig.custom()
//                设置超时时间
                .setConnectTimeout(5000)
                .build();
//      将设置载入
        get.setConfig(requestConfig);
//        get.setHeader();
//        get.addHeader();
        try {
            response = closeableHttpClient.execute(get);
            //获得响应结果
            HttpEntity entity = response.getEntity();
//            对HttpEntity操作的工具类
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println(result);
            //确保流关闭
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response!=null){
                    response.close();
                }
                if (get!=null){
                    get.releaseConnection();
                }
                if (closeableHttpClient!=null){
                    closeableHttpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    @Override
    public void getHaveParam(String url, Map<String, String> params) {

        CloseableHttpClient closeableHttpClient= HttpClientBuilder.create().build();

        CloseableHttpResponse response=null;

        HttpGet get=null;

        try {
            URIBuilder uriBuilder = new URIBuilder(url);

            /*
            第一种方法：
            对参数要做urlencode处理:一些特殊符号如+，空格，|在未编译的情况下会出现问题，
            如果是浏览器的话，浏览器会自动编译，在这里我们要手动处理。
            String param = URLEncoder.encode("参数", StandardCharsets.UTF_8);
            然后将参数拼接在路径后面
            下面这个是第二种方法：
            * */
//          对参数处理，拼接在路径后面
            if (params != null) {
                Set<Map.Entry<String, String>> entrySet = params.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {

                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            //创建请求的连接
            get=new HttpGet(uriBuilder.build());
            response = closeableHttpClient.execute(get);
            //获得响应结果
            HttpEntity entity = response.getEntity();
//            对HttpEntity操作的工具类
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println(result);
            //确保流关闭
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response!=null){
                    response.close();
                }
                if (get!=null){
                    get.releaseConnection();
                }
                if (closeableHttpClient!=null){
                    closeableHttpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
}
